import pygame
from pygame.locals import *
import math

pygame.init()
pygame.key.set_repeat(75, 0)
width,height = 640,480
screen= pygame.display.set_mode((width, height))

grass = pygame.image.load("resources/images/grass.png")
castle = pygame.image.load("resources/images/castle.png")
player = pygame.image.load("resources/images/dude.png")
arrow = pygame.image.load("resources/images/bullet.png")
badguyimg1 = pygame.image.load("resources/images/badguy.png")
badguyimg=badguyimg1

keys = [False,False,False,False]
playerpos = [100,100]
playerpos1 = [100,100]

acc = [0,0]
arrows = []

badtimer=100
badtimer1=0
badguys=[[640,100]]
healthvalue=194

def display():
    #draw background
    for x in range(width/grass.get_width()+1):
        for y in range(height/grass.get_height()+1):
            screen.blit(grass,(x*100,y*100))
    screen.blit(castle,(0,30))
    screen.blit(castle,(0,135))
    screen.blit(castle,(0,240))
    screen.blit(castle,(0,345 ))

    #draw player
    position = pygame.mouse.get_pos()
    angle = math.atan2(position[1]-(playerpos[1]),position[0]-(playerpos[0]))
    playerrot = pygame.transform.rotate(player, 360-angle*57.29)
    playerpos1 =(playerpos[0]-playerrot.get_rect().width/2, playerpos[1]-playerrot.get_rect().height/2)
    screen.blit(playerrot, playerpos1)

    #draw arrows
    for bullet in arrows:
        index = 0
        velx = math.cos(bullet[0])*10
        vely = math.sin(bullet[0])*10
        bullet[1] += velx
        bullet[2] += vely
        if bullet[1] < 0 or bullet[1] > width or bullet[2] < 0 or bullet[2] > height:
            arrows.pop(index)
        index += 1
        for projectile in arrows:
            arrow1 = pygame.transform.rotate(arrow, 360-projectile[0]*57.29)
            screen.blit(arrow1, (projectile[1], projectile[2]))

    #draw badguy
    global badtimer
    if badtimer == 0:
        badguys.append([width,random.randint(50,height-50)])
        badtimer = 100 - (badtimer1*2)
        if badtimer1 >= 35:
            badtimer1 = 35
        else:
            badtimer1 += 5
    index = 0
    for badguy in badguys:
        if badguy[0]<-64:
            badguys.pop(index)
        badguy[0] -=7
        index += 1
    for badguy in badguys:
         screen.blit(badguyimg,badguy)
        
    pygame.display.flip()

def key_down_event():
    if event.key == K_w:
        keys[0] = True
    elif event.key == K_a:
        keys[1] = True
    elif event.key == K_s:
        keys[2] = True
    elif event.key == K_d:
        keys[3] = True

def key_up_event():
    if event.key == K_w:
        keys[0] = False
    elif event.key ==K_a:
        keys[1] = False
    elif event.key == K_s:
        keys[2] = False
    elif event.key == K_d:
        keys[3] = False

def player_move():
    if keys[0]:
        playerpos[1]-=5
    elif keys[1]:
        playerpos[0]-=5
    elif keys[2]:
        playerpos[1]+=5
    elif keys[3]:
        playerpos[0]+=5

    if playerpos[0] < 0:
        playerpos[0] = 0
    elif playerpos[0] > width - player.get_width():
        playerpos[0] = width - player.get_width()
    
    if playerpos[1] < 0:
        playerpos[1] = 0
    elif playerpos[1] > height - player.get_height():
        playerpos[1] = height - player.get_height()
        
while 1:
    display()
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)
        elif event.type == pygame.KEYDOWN :
            key_down_event()
        elif event.type == pygame.KEYUP:
            key_up_event()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            position = pygame.mouse.get_pos()
            acc[1] += 1
            arrows.append([math.atan2(position[1]-(playerpos1[1]+32),position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos1[1]+32])
         

    player_move()

                

    
